package com.yupi.mianshiya.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.mianshiya.common.BaseResponse;
import com.yupi.mianshiya.common.ErrorCode;
import com.yupi.mianshiya.common.ResultUtils;
import com.yupi.mianshiya.exception.BusinessException;
import com.yupi.mianshiya.model.dto.announcement.AnnouncementQueryRequest;
import com.yupi.mianshiya.model.entity.Announcement;
import com.yupi.mianshiya.model.entity.User;
import com.yupi.mianshiya.service.AnnouncementService;
import com.yupi.mianshiya.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Api(tags = "公告接口")
@RestController
@RequestMapping("/announcement")
@Slf4j
public class AnnouncementController {
    @Resource
    private AnnouncementService announcementService;

    @Resource
    private UserService userService;

//    @ApiOperation(value = "管理员获取公告列表")
//    @GetMapping("/admin/get")
//    public BaseResponse<Page<Announcement>> getAnnouncementlistByAdmin(AnnouncementQueryRequest announcementQueryRequest, HttpServletRequest request) {
//        //校验参数
//        int current = announcementQueryRequest.getCurrent();
//        int size = announcementQueryRequest.getPageSize();
//
//        User loginUser = userService.getLoginUser(request);
//        if (!userService.isAdmin(loginUser)) {
//            throw new BusinessException(ErrorCode.NO_AUTH_ERROR, "无权限");
//        }
//        QueryWrapper<Announcement> queryWrapper = announcementService.getQueryWrapper(announcementQueryRequest);
//        Page<Announcement> page = new Page<>(current, size);
//        Page<Announcement> announcementPage = announcementService.page(page, queryWrapper);
//        return ResultUtils.success(announcementPage);
//
//    }

    @ApiOperation(value = "普通用户获取公告列表")
    @GetMapping("/get")
    public BaseResponse<Page<Announcement>> getAnnouncementlist(AnnouncementQueryRequest announcementQueryRequest, HttpServletRequest request) {
        //校验参数
        int current = announcementQueryRequest.getCurrent();
        int size = announcementQueryRequest.getPageSize();

        QueryWrapper<Announcement> queryWrapper = announcementService.getQueryWrapper(announcementQueryRequest);
        Page<Announcement> page = new Page<>(current, size);
        Page<Announcement> announcementPage = announcementService.page(page, queryWrapper);
        return ResultUtils.success(announcementPage);
    }
    @ApiOperation(value = "删除公告")
    @DeleteMapping("/delete")
    public BaseResponse deleteAnnouncement(Long id, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        if (!userService.isAdmin(loginUser)) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR, "无权限");
        }
        boolean b = announcementService.removeById(id);
        if (!b) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "删除失败");
        }

        return ResultUtils.success(true);
    }

    @ApiOperation("管理员修改公告")
    @PostMapping("/admin/update")
    public BaseResponse updateAnnouncement(@RequestBody Announcement announcement, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        if (!userService.isAdmin(loginUser)) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR, "无权限");
        }
        boolean b = announcementService.updateById(announcement);
        if (!b) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "修改失败");
        }
        return ResultUtils.success(true);
    }

    @ApiOperation("管理员添加公告")
    @PostMapping("/admin/add")
    public BaseResponse addAnnouncement(@RequestBody Announcement announcement, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        if (announcement.getPriority() < 0 || announcement.getPriority() > 10) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "优先级必须在0-10之间");
        }
        if (!userService.isAdmin(loginUser)) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR, "无权限");
        }
        boolean b = announcementService.save(announcement);
        if (!b) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "添加失败");
        }
        return ResultUtils.success(true);
    }

    @ApiOperation("管理员获取公告详情")
    @GetMapping("/admin/getByid")
    public BaseResponse<Announcement> getAnnouncementById(Long id, HttpServletRequest request) {
        userService.getLoginUser(request);

        Announcement announcement = announcementService.getById(id);
        if (announcement == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "公告不存在");
        }

        return ResultUtils.success(announcement);

    }


}
